# v2.0
# -*- coding: utf-8 -*-
import requests
import time



def check_sign_in_status(base_url, headers):
    api = "get_today_status"
    url = base_url + api
    response = requests.get(url, headers=headers)

    if response.status_code == 200:
        data = response.json()
        if data['err_no'] == 0:
            if data['data'] is True:
                print("【今日是否签到】", "已签到")
                return True
            elif data['data'] is False:
                print("【今日是否签到】", "未签到")
                return False
        else:
            print("【当前登录状态】", "未登录,请登录")
            pass
            return False
    else:
        print("【请求失败】", response.status_code)
        return False


def sign_in(base_url, params, headers):
    data = '{}'
    url = f"{base_url}check_in"
    response = requests.post(url, headers=headers, data=data,params=params)
    if response.status_code == 200:
        try:
            data = response.json()
            if data['err_no'] == 0 and data['err_msg'] == "success":
                print("【当前签到状态】", "签到成功")
                return True
            elif data['err_no'] == 3013 and data['err_msg'] == "掘金酱提示：签到失败了~":
                print("【当前签到状态】", data['err_msg'])
                return False
            elif data['err_no'] == 15001:
                print("【当前签到状态】", '重复签到')
                return True
            else:
                print("【当前签到状态】", data['err_msg'])
                return False
        except requests.JSONDecodeError:
            print("【签到功能】服务器返回的数据无法解析为JSON格式。")
            return False


def get_points(base_url, headers):
    api = "get_cur_point"
    url = base_url + api
    response = requests.get(url, headers=headers)

    if response.status_code == 200:
        try:
            data = response.json()
            if data['err_no'] == 0 and data['err_msg'] == "success":
                print("【矿石最新余额】", data['data'])
                return data['data']
        except requests.JSONDecodeError:
            print("【获取余额功能】服务器返回的数据无法解析为JSON格式。")
            return False


def get_free(base_url,params, headers):
    url = f"{base_url}lottery_config/get"
    response = requests.get(url, headers=headers, params=params)
    if response.status_code == 200:
        try:
            data = response.json()
            if data['err_no'] == 0 and data['err_msg'] == "success":
                if data['data']['free_count'] > 0:
                    print("【免费抽奖次数】", data['data']['free_count'])
                    return True
                else:
                    print("【免费抽奖次数】", data['data']['free_count'])
                    return False
        except requests.JSONDecodeError:
            print("【获取免费抽奖次数功能】服务器返回的数据无法解析为JSON格式。")
            return False


def draw(base_url, params, headers):
    # proxies = {
    # 'http': 'http://127.0.0.1:8080',
    # 'https': 'http://127.0.0.1:8080',
    # }
    url = f"{base_url}lottery/draw"
    data = '{}'
    response = requests.post(url, headers=headers, data=data,params=params)
    if response.status_code == 200:
        try:
            data = response.json()
            if data['err_no'] == 0 and data['err_msg'] == "success":
                print("【今日抽奖奖品】", data['data']['lottery_name'])
        except requests.JSONDecodeError:
            print("【抽奖功能】服务器返回的数据无法解析为JSON格式。")
            return False


def get_win(base_url, aid, uuid, spider, headers):
    api = "lottery_lucky/my_lucky"
    url = base_url + api
    data = {
        "aid": aid,
        "uuid": uuid,
        "spider": spider
    }
    response = requests.post(url, data=data, headers=headers)
    if response.status_code == 200:
        try:
            data = response.json()
            if data['err_no'] == 0 and data['err_msg'] == "success":
                total_value = data['data']['total_value']
                points = get_points(base_url, header1)
                cha = points - (6000 - total_value) * 20
                print("【当前幸运数值】：", total_value)
                if cha >= 0:
                    print("【距离中奖还差】：0 矿石！")
                elif cha <= 0:
                    print("【距离中奖还差】：", str(abs(cha)) + "矿石！")
        except requests.JSONDecodeError:
            print("【获取免费抽奖次数功能】服务器返回的数据无法解析为JSON格式。")
            return False
    else:
        print("【请求失败】", response.status_code)
        return False



if __name__ == "__main__":
    cookie = '__tea_cookie_tokens_2608=%257B%2522user_unique_id%2522%253A%25227278142983864976956%2522%252C%2522web_id%2522%253A%25227278142983864976956%2522%252C%2522timestamp%2522%253A1718032299972%257D; is_staff_user=false; store-region=cn-gd; store-region-src=uid; sid_guard=1779cac0c586a1dd00dbf676796fe4c8%7C1723624536%7C31536000%7CThu%2C+14-Aug-2025+08%3A35%3A36+GMT; uid_tt=d40479a34044e1e735a90dbb28ab1dac; uid_tt_ss=d40479a34044e1e735a90dbb28ab1dac; sid_tt=1779cac0c586a1dd00dbf676796fe4c8; sessionid=1779cac0c586a1dd00dbf676796fe4c8; sessionid_ss=1779cac0c586a1dd00dbf676796fe4c8; sid_ucp_v1=1.0.0-KDY3NmMwMjQ3NDViOTliNDc1ZDI0N2U3NWUwM2U4ODgyMjMwMjgzYTkKFwit07DA_fXhAhDY2PG1BhiwFDgCQPEHGgJsZiIgMTc3OWNhYzBjNTg2YTFkZDAwZGJmNjc2Nzk2ZmU0Yzg; ssid_ucp_v1=1.0.0-KDY3NmMwMjQ3NDViOTliNDc1ZDI0N2U3NWUwM2U4ODgyMjMwMjgzYTkKFwit07DA_fXhAhDY2PG1BhiwFDgCQPEHGgJsZiIgMTc3OWNhYzBjNTg2YTFkZDAwZGJmNjc2Nzk2ZmU0Yzg; passport_csrf_token=28a0a5ee50dec1d0e2346b66bffbfb1c; passport_csrf_token_default=28a0a5ee50dec1d0e2346b66bffbfb1c; _tea_utm_cache_2608={%22utm_source%22:%22infinitynewtab.com%22}; _tea_utm_cache_2018={%22utm_source%22:%22infinitynewtab.com%22}; csrf_session_id=ce20a8c5ee82c5865f2741173d9e1e4c'
    aid = "2608"
    uuid = "7278142983864976956"
    spider = "0"
    # msToken  获取后测试 url解码和未解码 哪种可以使用
    msToken = 'fn80ZuEAraOg-k3a4dhmDw_Qlz8xpPf6H8x6UYV_6Ft8yG1iQT8JM-rb2bWqKk3ki3zNnWrGvbd05qmkM7673xTGGA2YEXhNKbC52j0QMT0Sst_-OD7pGXWkQSgqR28='
    a_bogus = 'OjMdgc2pMsm1FjVzVhDz9rzmuPb0YW-XgZEzy12PY0wv'
    base_url = "https://api.juejin.cn/growth_api/v1/"

    common_params = {"aid": aid, "uuid": uuid, "spider": spider, "msToken": msToken, "a_bogus": a_bogus}
    header1 = {
        'Cookie': cookie,
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0'
    }
    # proxies = {
    #     'http': 'http://127.0.0.1:8080',
    #     'https': 'http://127.0.0.1:8080',
    # }
    if check_sign_in_status(base_url, header1):
        if get_free(base_url,common_params, header1):
            draw(base_url,common_params, header1)
            pass
        else:
            pass
    else:
        if not sign_in(base_url, common_params, header1):
            sign_in(base_url, common_params, header1)
        if get_free(base_url,common_params, header1):
            draw(base_url, common_params, header1)
            pass
        else:
            pass
    get_win(base_url, aid, uuid, spider, header1)
